package Verifier.ByteTree;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ofekp
 */
public class Node extends ByteTreeElement{
	List<ByteTreeElement> btElementArr;
	private static final boolean OUTPUT_ENABLE = false;

	
	/**
	 * The class constructor which creates an empty node that can contain other nodes or leafs.
	 * @param parent the parent of this node
	 */
	public Node(Node parent)  {
		super(parent);
		btElementArr = new LinkedList<ByteTreeElement>();
	}
	
	/**
	 * Inserts an element as this node child
	 * the insertion is ordered.
	 * @param e an element to add (node or leaf)
	 */
	public void addElement(ByteTreeElement e) {
		btElementArr.add(e);
	}
	
	/**
	 * @param index the index of the child (node or leaf)
	 * @return the node or leaf in the index requested (left first)
	 */
	public ByteTreeElement getChild(int index) {
		try {
			return btElementArr.get(index);
		} catch(IndexOutOfBoundsException i) {
			if (OUTPUT_ENABLE)
				System.out.println("Error: Node's list is shorter than the index requested.");
			return null;
		}
	}
	
	public Boolean isLeaf() {
		return false;
	}

	/**
	 * @return the number of children (nodes or leafs)
	 */
	public int getDataLength() {
		return btElementArr.size();
	}

	/**
	 * @return 	<pre>the byte string that represent this node
	 * 			including the '00' prefix, the number of children
	 * 			and the children that follows (nodes of leafs), all
	 * 			concatenated together.</pre>
	 */
	@Override
	public String toByteString() {
		String str = "00" + numTo8Char(getDataLength());
		for(ByteTreeElement elem : btElementArr) {
			str += elem.toByteString();
		}
		return str;
	}
}
